package com.zhang.third.day05;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.util.Collector;

/**
 * @title:
 * @author: zhang
 * @date: 2022/4/8 15:21
 */
public class Example5 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new SourceFunction<String>() {
                    @Override
                    public void run(SourceContext<String> ctx) throws Exception {
                        ctx.emitWatermark(new Watermark(-100 * 1000L));
                        ctx.collectWithTimestamp("hello", 1000L);
                        ctx.emitWatermark(new Watermark(100 * 1000L));
                    }

                    @Override
                    public void cancel() {

                    }
                })
                .keyBy(r -> 1)
                .process(new KeyedProcessFunction<Integer, String, String>() {
                    @Override
                    public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
                        out.collect("数据" + value + "到达，数据的时间戳是：" + ctx.timestamp() + "" +
                                "当前process算子的水位线是：" + ctx.timerService().currentWatermark());
                        ctx.timerService().registerEventTimeTimer(ctx.timestamp() + 10 * 1000L);
                        out.collect("注册了一个" + (ctx.timestamp() + 10 * 1000L) + "的定时器");
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                        out.collect("定时器" + timestamp + "触发了，当前process算子的水位线是：" +
                                "" + ctx.timerService().currentWatermark());
                    }
                })
                .print();

        env.execute();
    }
}
